package com.example.mapper.controller;

import com.example.mapper.domain.User;
import com.example.mapper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private UserService userService;


    @PostMapping
    public JData add(@RequestBody User user){
        userService.insert(user);
        return new JData(0,"ok",user.getId());
    }

    @GetMapping("/info")
    public JData getUserInfo(){
        User result = userService.selectByPrimaryKey(1);
        return new JData(0,"ok",result);
    }

    @DeleteMapping("/{id}")
    public JData delete(@PathVariable Integer id){
        Integer result = userService.deleteByPrimaryKey(id);
        return new JData(0,"ok",result);
    }

    @PutMapping
    public JData update(@RequestBody User user){
        Integer result = userService.updateByPrimaryKeySelective(user);
        return new JData(0,"OK",result);
    }

    @GetMapping("/{id}")
    public JData getFindById(@PathVariable Integer id){
        User result = userService.selectByPrimaryKey(id);
        return new JData(0,"ok",result);
    }

    @GetMapping("/getAll")
    public JData getAll(@RequestParam( value = "page",required = false) Integer page,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize){
        List<User> result = userService.getAll(page,pageSize);
        return new JData(0,"ok",result);
    }

    @GetMapping("/getList")
    public JData getList(@RequestParam( value = "page",required = false) Integer page,
                         @RequestParam(value = "pageSize",required = false) Integer pageSize,
                         @RequestParam(value = "q",required = false) String q,
                         @RequestParam(value = "name",required = false) String name,
                         @RequestParam(value = "username",required = false) String username,
                         @RequestParam(value = "age",required = false) String age ){

        Map<String,Object> params = new HashMap<String,Object>();
        if(page==null){
            page = 1;
        }
        params.put("page",page);
        if(pageSize==null||pageSize>=100||pageSize<=0){
            pageSize=20;
        }
        params.put("pageSize",pageSize);
        if(q!=null){
            params.put("q","%"+q+"%");
        }
        if(name!=null){
            params.put("name",name);
        }
        if(username!=null){
            params.put("username",username);
        }
        if(age!=null){
            params.put("age",age);
        }

        List<User> list = userService.getList(params);
        return new JData(0,"ok",list);
    }


    @RequestMapping("/hello")
    public String hello(){

        return "hello";
    }

    @RequestMapping("/userList")
    public String userList(){

        return "userList";
    }


    /**
     * 该方法是注册用户的方法，默认放开访问控制
     * @param user
     */
    @PostMapping("/signup")
    public User signUp(@RequestParam( value = "username",required = false) String username,
                         @RequestParam(value = "password",required = false) String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        System.out.println(user.getPassword());
        //applicationUserRepository.save(user);
        return user;
    }


}
